﻿namespace MapleRock.P2PEmulator.Core
{
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Net;
  using System.Text;

  public sealed class MessageSearchRequest : Message
  {
    public MessageSearchRequest(ulong uid, IPAddress backAddress, Guid requestId)
      : base(uid)
    {
      this.BackAddress = backAddress;
      this.RequestId = requestId;

      this.Ttl = 50;
    }

    public double[] Destination { get; set; }

    public double SearchRadius { get; set; }

    public IPAddress BackAddress { get; private set; }

    public Guid RequestId { get; private set; }

    public override int Size
    {
      get 
      {
        var size = Message.IpHeaderSize;
        size += sizeof(double);
        size += (sizeof(double) * this.Destination.Length);
        return size;
      }
    }

    public int Ttl { get; set; }

    public override void Assign(Message rhs)
    {
      Debug.Assert(rhs is MessageSearchRequest, "rhs is MessageSearchRequest");

      base.Assign(rhs);

      this.SearchRadius = ((MessageSearchRequest)rhs).SearchRadius;
      this.Destination = (double[])((MessageSearchRequest)rhs).Destination.Clone();
    }

    public override Message Duplicate(ulong uid)
    {
      Debug.Assert(null != this.Destination, "null != this.Destination");
      Debug.Assert(0 != this.SearchRadius, "0 != this.SearchRadius");

      var message = new MessageSearchRequest(uid, this.BackAddress, this.RequestId);
      message.Assign(this);
      return message;
    }
  }
}
